package com.vf.admin.client.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tencentcloudapi.ds.v20180523.models.DeleteSealRequest;
import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.common.ClientConstants;
import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.dto.FuJinRenDto;
import com.vf.admin.client.service.*;
import com.vf.admin.common.Constants;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.fiwork.util.DateUtils;
import com.vf.admin.fiwork.util.StringUtil;
import com.vf.admin.miaodong.entity.*;
import com.vf.admin.utils.DistanceUtils;
import com.vf.admin.utils.FuJinRenSelectUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import static com.vf.admin.common.Constants.banner_fangjian;
import static com.vf.admin.common.Constants.guizuCacheMap;
import static com.vf.admin.utils.StringEx.isNull;
import static com.vf.admin.utils.ConstellationUtil.getAgeByBirth;

/**
 * @author anxiang
 * @version 1.0.0
 * @ClassName AppFuJinRenController.java
 * @Description 获取附近人
 * @createTime 2021年07月22日 15:23:00
 */
@Controller
@RequestMapping("/appfujinren")
public class AppFuJinRenController extends ClientBaseController {

    @Autowired
    AppUserService appUserService;
    @Autowired
    AppUserDressupService appUserDressupService;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    private AppAuthUserService appAuthUserService;
    @Autowired
    private AppUserSettingService userSettingService;
    @Autowired
    private AppSsProvincesService provincesService;

    @Resource
    private AppSetupService appSetupService;

    /**
     * 自己用在线人员
     */
    @ResponseBody
    @RequestMapping("/searchList")
    public Result searchList(Integer sex, Integer pageno) {
        try {
            if (pageno == null) {
                return this.errorJSON("页码不存在！");
            }
            HashMap<String, Object> params = new HashMap<>();
            if (sex != null) {
                params.put("sex", sex);
            }
            params.put("onlinestatus", 1);


            List<AppUser> userList = null;
            SplitPageBean splitPageBean = new SplitPageBean(pageno, 20);
            userList = appUserService.list(params, splitPageBean);

            /**
             * updateTime: 20230711
             * author:zxw
             */

            if (userList == null || userList.size() == 0) {
                return this.sucessPage(new ArrayList<>(), splitPageBean.getNextPageNo());
            }
            String userArr = "";
            for (AppUser appUser : userList) {
                userArr += "," + appUser.getId();
            }
            if (!isNull(userArr)) {
                userArr = userArr.substring(1);
            }
            //批量查询发布人默认头饰
            List<AppUserDressup> userDressups = appUserDressupService.getPiliang(userArr, 2);
            Map<Integer, Integer> toushiMap = new HashMap<>(16);
            for (AppUserDressup userDressup : userDressups) {
                toushiMap.put(userDressup.getUserid(), userDressup.getMountsid());
            }

            //返回数据
            List<FuJinRenDto> fuJinRenDtoList = new ArrayList<>();
            for (AppUser appUser : userList) {

                FuJinRenDto fuJinRenDto = new FuJinRenDto();
                fuJinRenDto.setUserid(appUser.getId());
                //用户标识
                fuJinRenDto.setUsercode(appUser.getUsercode());
                // 用户头像
                fuJinRenDto.setUsericon(appUser.getPic());
                //用户昵称
                fuJinRenDto.setUsernick(appUser.getNick());
                //腾讯code
                fuJinRenDto.setTexcode(appUser.getTengxuncode());
                //用户性别
                fuJinRenDto.setUsersex(appUser.getSex());
                //用户签名
                fuJinRenDto.setQianming(appUser.getMysign());
                //年收入
                fuJinRenDto.setNianshouru(appUser.getNianshouru());
                //身高
                fuJinRenDto.setShengao(appUser.getShengao());
                //体重
                fuJinRenDto.setTizhong(appUser.getTizhong());
                //职业名
                fuJinRenDto.setZhiyename(appUser.getZhiyename());
                //年龄
                fuJinRenDto.setAge(getAgeByBirth(appUser.getBirthday()));
                //在线状态
                fuJinRenDto.setOnlinestatus(appUser.getOnlinestatus());
                //实名认证状态
                fuJinRenDto.setStatus(appUser.getStatus());
                if (appUser.getIsguizu() == 1) {
                    //是贵族
                    //根据贵族id  获取贵族图标
                    String medal = guizuCacheMap.get(appUser.getNobleid());
                    fuJinRenDto.setMedal(medal);
                } else {
                    fuJinRenDto.setMedal("");
                }
                //判断是否设置过头饰
                if (toushiMap.containsKey(appUser.getId())) {
                    //存在则放入
                    fuJinRenDto.setToushi(Constants.toushiCacheMap.get(toushiMap.get(appUser.getId())));
                } else {
                    //不存在则表示未点赞
                    fuJinRenDto.setToushi("");
                }
                if (!StringUtil.isNullOrEmpty(appUser.getAddress())) {
                    fuJinRenDto.setAddress(appUser.getAddress());
                }
                if (!StringUtil.isNullOrEmpty(appUser.getVoiceSignature())) {
                    fuJinRenDto.setVoiceSignature(appUser.getVoiceSignature());
                    fuJinRenDto.setVoiceSignatureSeconds(appUser.getVoiceSignatureSeconds());
                }


                Integer isReal = appAuthUserService.infoByUserAndName(appUser.getId(), ClientConstants.UserAuth.REAL_AUTH);
                fuJinRenDto.setIsReal(isReal);

                Map<String, Object> vipMap = appUserService.isVipMap(appUser.getId());
                fuJinRenDto.setVipMap(vipMap);
                fuJinRenDtoList.add(fuJinRenDto);

            }
            return this.sucessPage(fuJinRenDtoList, splitPageBean.getNextPageNo());
        } catch (Exception e) {
            e.printStackTrace();
            return this.errorJSON("系统繁忙,请联系管理员！");
        }

    }


    /**
     * @Description 获取附近人列表
     * @Param: [pageno] 页数
     * @return: com.vf.admin.common.entity.Result
     * @Author: ljx
     * @Date: 2021/07/22
     */
    @ResponseBody
    @RequestMapping("/list")
    public Result list(@RequestParam(value = "lon", required = false) Double lon, @RequestParam(value = "lat", required = false) Double lat, Integer pageno) {
        try {
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            List<FuJinRenDto> fuJinRenDtoList = new ArrayList<>();
            SplitPageBean splitPageBean = new SplitPageBean(pageno, 20);

            if (pageno == null) {
                return this.errorJSON("页码不存在！");
            }
//            if (lon == null || lat == null) {
//                return this.sucessPage(fuJinRenDtoList, 1);
//            }
            //筛掉隐藏位置信息的用户
            List<Map> userIdsByHiddenArea = userSettingService.getUserIdsByHiddenArea();
            String hiddenUserIds = userIdsByHiddenArea.stream().map(e -> e.get("userId").toString()).collect(Collectors.joining(","));

            HashMap<String, Object> params = new HashMap<>();

            List<AppUser> userList = null;
            params.put("lon", lon);
            params.put("lat", lat);
            if (StringUtils.isNotBlank(hiddenUserIds)) {
                params.put("hiddenUserIds", hiddenUserIds + "," + getUserId());
            } else {
                params.put("hiddenUserIds", getUserId());
            }

            /**
             * updateTime: 20230711
             * author:zxw
             */
            //1男 2女
            //男号返回女号  女号返回男号   未知返回女号
            if (ObjectUtil.isNotEmpty(user)) {

                if (user.getSex().equals(1)) {
                    params.put("sex", 2);
                    params.put("isReal", 0);
                }
                if (user.getSex().equals(2)) {
                    params.put("sex", 1);
                    params.put("isReal", 1);
                }
                if (user.getSex().equals(3)) {
                    params.put("sex", 2);
                    params.put("isReal", 0);
                }

            }

//            //判断平台
//            String header = request.getHeader(ClientConstants.packageName);
//
//            if (ObjectUtil.isNotEmpty(header)) {
//                params.put("packageName", header);
//            }else {
//                params.put("packageName", "lyq");
//            }


            //状态 是否禁用  1、正常   2、禁用  默认为1、正常
            params.put("states", 1);

            //审查
            AppSetup appSetup = appSetupService.get();
            if (ObjectUtil.isNotEmpty(appSetup)) {

                if (appSetup.getAuditStatus().equals(1)) {
                    String ids = appSetup.getUserIds();
                    params.put("ids", ids);

                }
            }

            //
            userList = appUserService.getFujinRenList(params, splitPageBean);
            //
            if (userList == null || userList.size() == 0) {
                return this.sucessPage(new ArrayList<>(), splitPageBean.getNextPageNo());
            }
            //
            List<AppUser> filteredList = userList.stream()
                    .filter(s -> !s.getSex().equals(user.getSex()))
                    .collect(Collectors.toList());
            String userArr = "";
            for (AppUser appUser : filteredList) {
                userArr += "," + appUser.getId();
            }
            if (!isNull(userArr)) {
                userArr = userArr.substring(1);
            }
            //批量查询发布人默认头饰
            List<AppUserDressup> userDressups = appUserDressupService.getPiliang(userArr, 2);
            Map<Integer, Integer> toushiMap = new HashMap<>(16);
            for (AppUserDressup userDressup : userDressups) {
                toushiMap.put(userDressup.getUserid(), userDressup.getMountsid());
            }

            for (AppUser appUser : filteredList) {

                FuJinRenDto fuJinRenDto = new FuJinRenDto();
                fuJinRenDto.setUserid(appUser.getId());
                //用户标识
                fuJinRenDto.setUsercode(appUser.getUsercode());
                // 用户头像
                fuJinRenDto.setUsericon(appUser.getPic());
                //用户昵称
                fuJinRenDto.setUsernick(appUser.getNick());
                //腾讯code
                fuJinRenDto.setTexcode(appUser.getTengxuncode());
                //用户性别
                fuJinRenDto.setUsersex(appUser.getSex());
                //用户签名
                fuJinRenDto.setQianming(appUser.getMysign());
                //年收入
                fuJinRenDto.setNianshouru(appUser.getNianshouru());
                //身高
                fuJinRenDto.setShengao(appUser.getShengao());
                //体重
                fuJinRenDto.setTizhong(appUser.getTizhong());
                //职业名
                fuJinRenDto.setZhiyename(appUser.getZhiyename());
                //年龄
                fuJinRenDto.setAge(getAgeByBirth(appUser.getBirthday()));
                //在线状态
                fuJinRenDto.setOnlinestatus(appUser.getOnlinestatus());
                //实名认证状态
                fuJinRenDto.setStatus(appUser.getStatus());
                if (appUser.getIsguizu() == 1) {
                    //是贵族
                    //根据贵族id  获取贵族图标
                    String medal = guizuCacheMap.get(appUser.getNobleid());
                    fuJinRenDto.setMedal(medal);
                } else {
                    fuJinRenDto.setMedal("");
                }
                fuJinRenDto.setJuli("未知");
                Map<String, Object> setting = userSettingService.get(appUser.getId());
                if (!Objects.isNull(setting)) {
                    if (setting.get("hiddenArea") != null && Integer.valueOf(setting.get("hiddenArea").toString()) == 1 && lon != null && lat != null && StringUtils.isNotBlank(appUser.getLon()) && StringUtils.isNotBlank(appUser.getLat())) {
                        double distance = DistanceUtils.getDistance(lon, lat, Double.valueOf(appUser.getLon()), Double.valueOf(appUser.getLat()));
                        fuJinRenDto.setJuli(distance + "km");
                    } else if (Integer.valueOf(setting.get("hiddenArea").toString()) == 2) {
                        if (appUser.getShiid() != null) {
                            SsProvinces ssProvinces = provincesService.get(appUser.getShiid());
                            fuJinRenDto.setJuli(ssProvinces.getProvince());
                        }
                    }
                }
                //判断是否设置过头饰
                if (toushiMap.containsKey(appUser.getId())) {
                    //存在则放入
                    fuJinRenDto.setToushi(Constants.toushiCacheMap.get(toushiMap.get(appUser.getId())));
                } else {
                    //不存在则表示未点赞
                    fuJinRenDto.setToushi("");
                }
                if (!StringUtil.isNullOrEmpty(appUser.getAddress())) {
                    fuJinRenDto.setAddress(appUser.getAddress());
                }
                if (!StringUtil.isNullOrEmpty(appUser.getVoiceSignature())) {
                    fuJinRenDto.setVoiceSignature(appUser.getVoiceSignature());
                    fuJinRenDto.setVoiceSignatureSeconds(appUser.getVoiceSignatureSeconds());
                }

                if (redisUtil.hasKey(RedisKeys.BECKONING + getUserId() + "-" + appUser.getId())) {
                    fuJinRenDto.setIsBeckoning(1);
                } else {
                    fuJinRenDto.setIsBeckoning(2);
                }

                Integer isReal = appAuthUserService.infoByUserAndName(appUser.getId(), ClientConstants.UserAuth.REAL_AUTH);
                fuJinRenDto.setIsReal(isReal);

                Map<String, Object> vipMap = appUserService.isVipMap(appUser.getId());
                fuJinRenDto.setVipMap(vipMap);

                fuJinRenDtoList.add(fuJinRenDto);
            }
            return this.sucessPage(fuJinRenDtoList, splitPageBean.getNextPageNo());
        } catch (Exception e) {
            e.printStackTrace();
            return this.errorJSON("系统繁忙,请联系管理员！");
        }
    }

    /**
     * 同城
     *
     * @param realAuthStatus
     * @param cityCode
     * @param eduStatus
     * @param ageMin
     * @param ageMax
     * @param pageno
     * @return
     */
    @ResponseBody
    @RequestMapping("/recommentList")
//    public Result recommentList(Integer type, Integer realAuthStatus, Integer cityCode, Integer eduStatus, Integer ageMin, Integer ageMax, Integer pageno) {
    public Result recommentList(Integer type,Integer pageno) {

        try {

            if (pageno == null) {
                return this.errorJSON("页码不存在！");
            }
            HashMap<String, Object> params = new HashMap<>();
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            //
//            if (ObjectUtil.isNotEmpty(user)) {
//
//                if (user.getSex().equals(1)) {
//                    params.put("sex", 2);
//                    params.put("isReal", 0);
//                }
//                if (user.getSex().equals(2)) {
//                    params.put("sex", 1);
//                    params.put("isReal", 1);
//                }
//                if (user.getSex().equals(3)) {
//                    params.put("sex", 2);
//                    params.put("isReal", 0);
//                }
//
//            }

            // sex: 1、男  2、女
            // vip: 1、会员
            if (ObjectUtil.isNotEmpty(user)) {
                //附近
                if (type.equals(1)) {
                    //会员
                    if (user.getVip().equals(1)) {
                        if (user.getSex().equals(2)) {
                            //女
                            params.put("sex", 1);
                            params.put("isReal", 1);
                        }else{
                            //男
                            params.put("sex", 2);
                            params.put("isNearVip", 1);//数据0+真实号
                        }
                    }else{
                        //非会员
                        if (user.getSex().equals(2)) {
                            //女
                            params.put("sex", 1);
                            params.put("isReal", 1);
                        }else{
                            //男
                            params.put("sex", 2);
                            params.put("isReal", 2);
                        }
                    }
                }
//                params.put("pic", "http://osszhiyuhn1.ptszyxx.com/default/nv.png");
//                params.put("pic", "https://yunliaos.oss-cn-beijing.aliyuncs.com/image/default_head.png");
                //新注册
                if (type.equals(2)) {
                    if (user.getSex().equals(2)) {
                        //女
                        params.put("sex", 1);
                        params.put("isReal", 1);
                    }else{
                        //男
                        if (user.getVip().equals(1)) {
                            params.put("sex", 2);
                            params.put("isReal", 1);
                        }else{
                            params.put("sex", 2);
                            params.put("isReal", 0);
                        }
                    }
                }
                //新萌
                if (type.equals(3)) {
                    if (user.getSex().equals(2)) {
                        params.put("sex", 1);
                        params.put("isReal", 1);
                    }else{
                        params.put("sex", 2);
                        params.put("isReal", 1);
                    }
                }
            }

            params.put("states", 1); //状态 是否禁用  1、正常   2、禁用  默认为1、正常
            SplitPageBean splitPageBean = new SplitPageBean(pageno, 20);
            List<AppUser> userList = appUserService.list(params, splitPageBean);
            Collections.shuffle(userList);//用户列表洗牌
            if (userList.size() == 0) {
                return this.sucessPage(new ArrayList<>(), splitPageBean.getNextPageNo());
            }
            String userArr = "";
            for (AppUser appUser : userList) {
                userArr += "," + appUser.getId();
            }
            if (!isNull(userArr)) {
                userArr = userArr.substring(1);
            }
            //批量查询发布人默认头饰
            List<AppUserDressup> userDressups = appUserDressupService.getPiliang(userArr, 2);
            Map<Integer, Integer> toushiMap = new HashMap<>(16);
            for (AppUserDressup userDressup : userDressups) {
                toushiMap.put(userDressup.getUserid(), userDressup.getMountsid());
            }
            //返回数据
            List<FuJinRenDto> fuJinRenDtoList = new ArrayList<>();
            for (AppUser appUser : userList) {
                if (appUser.getId().equals(user.getId())) {
                    continue;
                }
                FuJinRenDto fuJinRenDto = new FuJinRenDto();
                fuJinRenDto.setUserid(appUser.getId());
                //用户标识
                fuJinRenDto.setUsercode(appUser.getUsercode());
                // 用户头像
                fuJinRenDto.setUsericon(appUser.getPic());
                //用户昵称
                fuJinRenDto.setUsernick(appUser.getNick());
                //腾讯code
                fuJinRenDto.setTexcode(appUser.getTengxuncode());
                //用户性别
                fuJinRenDto.setUsersex(appUser.getSex());
                //用户签名
                fuJinRenDto.setQianming(appUser.getMysign());
                //年收入
                fuJinRenDto.setNianshouru(appUser.getNianshouru());
                //身高
                fuJinRenDto.setShengao(appUser.getShengao());
                //体重
                fuJinRenDto.setTizhong(appUser.getTizhong());
                //职业名
                fuJinRenDto.setZhiyename(appUser.getZhiyename());
                //年龄
                fuJinRenDto.setAge(getAgeByBirth(appUser.getBirthday()));
                //在线状态
                fuJinRenDto.setOnlinestatus(appUser.getOnlinestatus());
                //实名认证状态
                fuJinRenDto.setStatus(appUser.getStatus());
                if (appUser.getIsguizu() == 1) {
                    //是贵族
                    //根据贵族id  获取贵族图标
                    String medal = guizuCacheMap.get(appUser.getNobleid());
                    fuJinRenDto.setMedal(medal);
                } else {
                    fuJinRenDto.setMedal("");
                }
                //判断是否设置过头饰
                if (toushiMap.containsKey(appUser.getId())) {
                    //存在则放入
                    fuJinRenDto.setToushi(Constants.toushiCacheMap.get(toushiMap.get(appUser.getId())));
                } else {
                    //不存在则表示未点赞
                    fuJinRenDto.setToushi("");
                }
                if (!StringUtil.isNullOrEmpty(appUser.getAddress())) {
                    fuJinRenDto.setAddress(appUser.getAddress());
                }
                if (!StringUtil.isNullOrEmpty(appUser.getVoiceSignature())) {
                    fuJinRenDto.setVoiceSignature(appUser.getVoiceSignature());
                    fuJinRenDto.setVoiceSignatureSeconds(appUser.getVoiceSignatureSeconds());
                }

                if (redisUtil.hasKey(RedisKeys.BECKONING + user.getId() + "-" + appUser.getId())) {
                    fuJinRenDto.setIsBeckoning(1);
                } else {
                    fuJinRenDto.setIsBeckoning(2);
                }

                Integer isReal = appAuthUserService.infoByUserAndName(appUser.getId(), ClientConstants.UserAuth.REAL_AUTH);
                fuJinRenDto.setIsReal(isReal);

                //
                if (ObjectUtil.isEmpty(appUser.getWeChatNum())) {
                    fuJinRenDto.setWeChatNum("");
                } else {
                    fuJinRenDto.setWeChatNum(appUser.getWeChatNum());
                }
                if (ObjectUtil.isEmpty(appUser.getQqNum())) {
                    fuJinRenDto.setQqNum("");
                } else {
                    fuJinRenDto.setQqNum(appUser.getQqNum());
                }
                if (ObjectUtil.isEmpty(appUser.getPhoneNum())) {
                    fuJinRenDto.setPhoneNum("");
                } else {
                    fuJinRenDto.setPhoneNum(appUser.getPhoneNum());
                }
                //判断形象墙 是否存在
                if (!isNull(appUser.getXingxiang())) {
                    //形象墙存在 ,切割
                    String[] arr = appUser.getXingxiang().split(",");
                    if (arr.length > 0) {
                        // /移入数组
                        fuJinRenDto.setXingxiangList(Arrays.asList(arr));
                    }
                } else {
                    //不存在  数组为空
                    fuJinRenDto.setXingxiangList(Arrays.asList());
                }

                Map<String, Object> vipMap = appUserService.isVipMap(appUser.getId());
                fuJinRenDto.setVipMap(vipMap);
                fuJinRenDtoList.add(fuJinRenDto);

            }
            return this.sucessPage(fuJinRenDtoList, splitPageBean.getNextPageNo());
        } catch (Exception e) {
            e.printStackTrace();
            return this.errorJSON("系统繁忙,请联系管理员！");
        }
    }

//    @ResponseBody
//    @RequestMapping("/recommentList")
//    public Result recommentList(Integer type,Integer realAuthStatus, Integer cityCode, Integer eduStatus, Integer ageMin, Integer ageMax, Integer pageno) {
//
//        try {
//
//            if (pageno == null) {
//                return this.errorJSON("页码不存在！");
//            }
//
//            HashMap<String, Object> params = new HashMap<>();
//
////            if (realAuthStatus != null && realAuthStatus != 0) {
////                params.put("realAuthStatus", realAuthStatus);
////            }
////            if (cityCode != null) {
////                params.put("cityCode", cityCode);
////            }
////            if (eduStatus != null) {
////                params.put("eduStatus", eduStatus);
////            }
////            if (ageMin != null && ageMin != 0 && ageMax != null && ageMax != 0) {
////                if (ageMin > ageMax) {
////                    return this.errorJSON("年龄筛选错误！");
////                }
////                String s = DateUtils.subYear(new Date(), ageMax - 1);
////                s = s.substring(0, 4) + "-01-" + s.substring(8);
////                String s1 = DateUtils.subYear(new Date(), ageMin - 1);
////                params.put("start", s);
////                params.put("end", s1);
////            }
//            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
//            //获取当前登录用户的user对象
//            AppUser user = appUserService.get(userId);
//            //
//            if (ObjectUtil.isNotEmpty(user)) {
//
//                if (user.getSex().equals(1)) {
//                    params.put("sex", 2);
//                    params.put("isReal", 0);
//                }
//                if (user.getSex().equals(2)) {
//                    params.put("sex", 1);
//                    params.put("isReal", 1);
//                }
//                if (user.getSex().equals(3)) {
//                    params.put("sex", 2);
//                    params.put("isReal", 0);
//                }
//
//
//
//
//            }
//
//
//
//
//            //判断平台
//            String header = request.getHeader(ClientConstants.packageName);
//            System.out.println("平台：" + header);
////            if (ObjectUtil.isNotEmpty(header)) {
////                params.put("packageName", header);
////            }else {
////                params.put("packageName", "lyq");
////            }
//
//            //状态 是否禁用  1、正常   2、禁用  默认为1、正常
//            params.put("states", 1);
//
//            //审查
//            AppSetup appSetup = appSetupService.get();
//            if (ObjectUtil.isNotEmpty(appSetup)) {
//
//                if (appSetup.getAuditStatus().equals(1)) {
//                    String ids = appSetup.getUserIds();
//                    params.put("ids", ids);
//
//                }
//            }
//
//            List<AppUser> userList = null;
//            SplitPageBean splitPageBean = new SplitPageBean(pageno, 20);
//            userList = appUserService.list(params, splitPageBean);
//
//            Collections.shuffle(userList);
//
//            if (userList.size() == 0) {
//                return this.sucessPage(new ArrayList<>(), splitPageBean.getNextPageNo());
//            }
//            String userArr = "";
//            for (AppUser appUser : userList) {
//                userArr += "," + appUser.getId();
//            }
//            if (!isNull(userArr)) {
//                userArr = userArr.substring(1);
//            }
//            //批量查询发布人默认头饰
//            List<AppUserDressup> userDressups = appUserDressupService.getPiliang(userArr, 2);
//            Map<Integer, Integer> toushiMap = new HashMap<>(16);
//            for (AppUserDressup userDressup : userDressups) {
//                toushiMap.put(userDressup.getUserid(), userDressup.getMountsid());
//            }
//            //返回数据
//            List<FuJinRenDto> fuJinRenDtoList = new ArrayList<>();
//            for (AppUser appUser : userList) {
//                if (appUser.getId().equals(user.getId())) {
//                    continue;
//                }
//                FuJinRenDto fuJinRenDto = new FuJinRenDto();
//                fuJinRenDto.setUserid(appUser.getId());
//                //用户标识
//                fuJinRenDto.setUsercode(appUser.getUsercode());
//                // 用户头像
//                fuJinRenDto.setUsericon(appUser.getPic());
//                //用户昵称
//                fuJinRenDto.setUsernick(appUser.getNick());
//                //腾讯code
//                fuJinRenDto.setTexcode(appUser.getTengxuncode());
//                //用户性别
//                fuJinRenDto.setUsersex(appUser.getSex());
//                //用户签名
//                fuJinRenDto.setQianming(appUser.getMysign());
//                //年收入
//                fuJinRenDto.setNianshouru(appUser.getNianshouru());
//                //身高
//                fuJinRenDto.setShengao(appUser.getShengao());
//                //体重
//                fuJinRenDto.setTizhong(appUser.getTizhong());
//                //职业名
//                fuJinRenDto.setZhiyename(appUser.getZhiyename());
//                //年龄
//                fuJinRenDto.setAge(getAgeByBirth(appUser.getBirthday()));
//                //在线状态
//                fuJinRenDto.setOnlinestatus(appUser.getOnlinestatus());
//                //实名认证状态
//                fuJinRenDto.setStatus(appUser.getStatus());
//                if (appUser.getIsguizu() == 1) {
//                    //是贵族
//                    //根据贵族id  获取贵族图标
//                    String medal = guizuCacheMap.get(appUser.getNobleid());
//                    fuJinRenDto.setMedal(medal);
//                } else {
//                    fuJinRenDto.setMedal("");
//                }
//                //判断是否设置过头饰
//                if (toushiMap.containsKey(appUser.getId())) {
//                    //存在则放入
//                    fuJinRenDto.setToushi(Constants.toushiCacheMap.get(toushiMap.get(appUser.getId())));
//                } else {
//                    //不存在则表示未点赞
//                    fuJinRenDto.setToushi("");
//                }
//                if (!StringUtil.isNullOrEmpty(appUser.getAddress())) {
//                    fuJinRenDto.setAddress(appUser.getAddress());
//                }
//                if (!StringUtil.isNullOrEmpty(appUser.getVoiceSignature())) {
//                    fuJinRenDto.setVoiceSignature(appUser.getVoiceSignature());
//                    fuJinRenDto.setVoiceSignatureSeconds(appUser.getVoiceSignatureSeconds());
//                }
//
//                if (redisUtil.hasKey(RedisKeys.BECKONING + user.getId() + "-" + appUser.getId())) {
//                    fuJinRenDto.setIsBeckoning(1);
//                } else {
//                    fuJinRenDto.setIsBeckoning(2);
//                }
//
//                Integer isReal = appAuthUserService.infoByUserAndName(appUser.getId(), ClientConstants.UserAuth.REAL_AUTH);
//                fuJinRenDto.setIsReal(isReal);
//
//                Map<String, Object> vipMap = appUserService.isVipMap(appUser.getId());
//                fuJinRenDto.setVipMap(vipMap);
//                fuJinRenDtoList.add(fuJinRenDto);
//
//            }
//            return this.sucessPage(fuJinRenDtoList, splitPageBean.getNextPageNo());
//        } catch (Exception e) {
//            e.printStackTrace();
//            return this.errorJSON("系统繁忙,请联系管理员！");
//        }
//    }

    /**
     * 广场
     *
     * @param realAuthStatus
     * @param cityCode
     * @param eduStatus
     * @param ageMin
     * @param ageMax
     * @param pageno
     * @return
     */
    @ResponseBody
    @RequestMapping("/squareList")
    public Result squareList(Integer realAuthStatus, Integer cityCode, Integer eduStatus, Integer ageMin, Integer ageMax, Integer pageno) {
        try {
            if (pageno == null) {
                return this.errorJSON("页码不存在！");
            }
            HashMap<String, Object> params = new HashMap<>();

            if (realAuthStatus != null && realAuthStatus != 0) {
                params.put("realAuthStatus", realAuthStatus);
            }
            if (cityCode != null) {
                params.put("cityCode", cityCode);
            }
            if (eduStatus != null) {
                params.put("eduStatus", eduStatus);
            }
            if (ageMin != null && ageMin != 0 && ageMax != null && ageMax != 0) {
                if (ageMin > ageMax) {
                    return this.errorJSON("年龄筛选错误！");
                }
                String s = DateUtils.subYear(new Date(), ageMax - 1);
                s = s.substring(0, 4) + "-01-" + s.substring(8);
                String s1 = DateUtils.subYear(new Date(), ageMin - 1);
                params.put("start", s);
                params.put("end", s1);
            }

            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            /**
             * updateTime: 20230711
             * author:zxw
             */
            if (ObjectUtil.isNotEmpty(user)) {

                if (user.getSex().equals(1)) {
                    params.put("sex", 2);
                    params.put("isReal", 0);
                }
                if (user.getSex().equals(2)) {
                    params.put("sex", 1);
                    params.put("isReal", 1);
                }
                if (user.getSex().equals(3)) {
                    params.put("sex", 2);
                    params.put("isReal", 0);
                }

            }
            params.put("states", 1);

            List<AppUser> userList = null;
            SplitPageBean splitPageBean = new SplitPageBean(pageno, 20);
            userList = appUserService.list(params, splitPageBean);
            Collections.shuffle(userList);
            if (userList == null || userList.size() == 0) {
                return this.sucessPage(new ArrayList<>(), splitPageBean.getNextPageNo());
            }
            String userArr = "";
            for (AppUser appUser : userList) {
                userArr += "," + appUser.getId();
            }
            if (!isNull(userArr)) {
                userArr = userArr.substring(1);
            }
            //批量查询发布人默认头饰
            List<AppUserDressup> userDressups = appUserDressupService.getPiliang(userArr, 2);
            Map<Integer, Integer> toushiMap = new HashMap<>(16);
            for (AppUserDressup userDressup : userDressups) {
                toushiMap.put(userDressup.getUserid(), userDressup.getMountsid());
            }

            //返回数据
            List<FuJinRenDto> fuJinRenDtoList = new ArrayList<>();
            for (AppUser appUser : userList) {
                if (appUser.getId().equals(user.getId())) {
                    continue;
                }
                FuJinRenDto fuJinRenDto = new FuJinRenDto();
                fuJinRenDto.setUserid(appUser.getId());
                //用户标识
                fuJinRenDto.setUsercode(appUser.getUsercode());
                // 用户头像
                fuJinRenDto.setUsericon(appUser.getPic());
                //用户昵称
                fuJinRenDto.setUsernick(appUser.getNick());
                //腾讯code
                fuJinRenDto.setTexcode(appUser.getTengxuncode());
                //用户性别
                fuJinRenDto.setUsersex(appUser.getSex());
                //用户签名
                fuJinRenDto.setQianming(appUser.getMysign());
                //年收入
                fuJinRenDto.setNianshouru(appUser.getNianshouru());
                //身高
                fuJinRenDto.setShengao(appUser.getShengao());
                //体重
                fuJinRenDto.setTizhong(appUser.getTizhong());
                //职业名
                fuJinRenDto.setZhiyename(appUser.getZhiyename());
                //年龄
                fuJinRenDto.setAge(getAgeByBirth(appUser.getBirthday()));
                //在线状态
                fuJinRenDto.setOnlinestatus(appUser.getOnlinestatus());
                //实名认证状态
                fuJinRenDto.setStatus(appUser.getStatus());
                if (appUser.getIsguizu() == 1) {
                    //是贵族
                    //根据贵族id  获取贵族图标
                    String medal = guizuCacheMap.get(appUser.getNobleid());
                    fuJinRenDto.setMedal(medal);
                } else {
                    fuJinRenDto.setMedal("");
                }
                //判断是否设置过头饰
                if (toushiMap.containsKey(appUser.getId())) {
                    //存在则放入
                    fuJinRenDto.setToushi(Constants.toushiCacheMap.get(toushiMap.get(appUser.getId())));
                } else {
                    //不存在则表示未点赞
                    fuJinRenDto.setToushi("");
                }
                if (!StringUtil.isNullOrEmpty(appUser.getAddress())) {
                    fuJinRenDto.setAddress(appUser.getAddress());
                }
                if (!StringUtil.isNullOrEmpty(appUser.getVoiceSignature())) {
                    fuJinRenDto.setVoiceSignature(appUser.getVoiceSignature());
                    fuJinRenDto.setVoiceSignatureSeconds(appUser.getVoiceSignatureSeconds());
                }

                if (redisUtil.hasKey(RedisKeys.BECKONING + user.getId() + "-" + appUser.getId())) {
                    fuJinRenDto.setIsBeckoning(1);
                } else {
                    fuJinRenDto.setIsBeckoning(2);
                }

                Integer isReal = appAuthUserService.infoByUserAndName(appUser.getId(), ClientConstants.UserAuth.REAL_AUTH);
                fuJinRenDto.setIsReal(isReal);

                Map<String, Object> vipMap = appUserService.isVipMap(appUser.getId());
                fuJinRenDto.setVipMap(vipMap);
                fuJinRenDtoList.add(fuJinRenDto);

            }
            return this.sucessPage(fuJinRenDtoList, splitPageBean.getNextPageNo());
        } catch (Exception e) {
            e.printStackTrace();
            return this.errorJSON("系统繁忙,请联系管理员！");
        }
    }


    /**
     * 寻找附近的人
     */
    @ResponseBody
    @RequestMapping("/xunzhaofujinderen")
    public Result xunzhaofujinderen(String address) {

        try {
            //获取用户token
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            //获取获取条数
            Integer selectPageSize = FuJinRenSelectUtil.getRenNum(address);
            //返回数据
            List<AppUser> userList = null;
            //返回前端数据
            List<FuJinRenDto> fuJinRenDtoList = new ArrayList<>();
            //判断账号 sex
            //男号或是未知 返回女号
            if (user.getSex().equals(1) || user.getSex().equals(3)) {

                //查询redis统计条数
                Object object = redisUtil.get(RedisKeys.USER_COUNT_GIRL);
                //判断是否为空
                if (ObjectUtil.isNotEmpty(object)) {
                    //查询操作
                    HashMap<String, Object> params = new HashMap<>();
                    //账号状态
                    params.put("states", 1);
                    //是否真实用户
                    params.put("isReal", 0);
                    //查询女号
                    params.put("sex", 2);
                    //获取缓存总条数记录
                    Integer countInt = Integer.valueOf(object.toString());
                    //总页数 / 每页多少条
                    int randomNum = countInt / selectPageSize + (countInt % selectPageSize != 0 ? 1 : 0);
                    Random random = new Random();
                    int nextInt = random.nextInt(randomNum);
                    SplitPageBean splitPageBean = new SplitPageBean(nextInt, selectPageSize);
                    userList = appUserService.list(params, splitPageBean);

                    for (AppUser appUser : userList) {

                        FuJinRenDto fuJinRenDto = new FuJinRenDto();
                        fuJinRenDto.setUserid(appUser.getId());
                        //用户标识
                        fuJinRenDto.setUsercode(appUser.getUsercode());
                        // 用户头像
                        fuJinRenDto.setUsericon(appUser.getPic());
                        //用户昵称
                        fuJinRenDto.setUsernick(appUser.getNick());
                        //腾讯code
                        fuJinRenDto.setTexcode(appUser.getTengxuncode());
                        //用户性别
                        fuJinRenDto.setUsersex(appUser.getSex());
                        //用户签名
                        fuJinRenDto.setQianming(appUser.getMysign());
                        //年收入
                        fuJinRenDto.setNianshouru(appUser.getNianshouru());
                        //身高
                        fuJinRenDto.setShengao(appUser.getShengao());
                        //体重
                        fuJinRenDto.setTizhong(appUser.getTizhong());
                        //职业名
                        fuJinRenDto.setZhiyename(appUser.getZhiyename());
                        //年龄
                        fuJinRenDto.setAge(getAgeByBirth(appUser.getBirthday()));
                        //在线状态
                        fuJinRenDto.setOnlinestatus(appUser.getOnlinestatus());
                        //实名认证状态
                        fuJinRenDto.setStatus(appUser.getStatus());
                        fuJinRenDtoList.add(fuJinRenDto);
                    }
                } else {

                    //查询操作
                    HashMap<String, Object> params = new HashMap<>();
                    //账号状态
                    params.put("states", 1);
                    //是否真实用户
                    params.put("isReal", 0);
                    //查询女号
                    params.put("sex", 2);
                    //获取条数
                    int countInt = appUserService.userCount(params).size();
                    //查询条数
                    redisUtil.set(RedisKeys.USER_COUNT_GIRL, countInt, 3600);
                    //总页数 / 每页多少条
                    int randomNum = countInt / selectPageSize + (countInt % selectPageSize != 0 ? 1 : 0);
                    Random random = new Random();
                    int nextInt = random.nextInt(randomNum);
                    SplitPageBean splitPageBean = new SplitPageBean(nextInt, selectPageSize);
                    userList = appUserService.list(params, splitPageBean);
                    for (AppUser appUser : userList) {
                        FuJinRenDto fuJinRenDto = new FuJinRenDto();
                        fuJinRenDto.setUserid(appUser.getId());
                        //用户标识
                        fuJinRenDto.setUsercode(appUser.getUsercode());
                        // 用户头像
                        fuJinRenDto.setUsericon(appUser.getPic());
                        //用户昵称
                        fuJinRenDto.setUsernick(appUser.getNick());
                        //腾讯code
                        fuJinRenDto.setTexcode(appUser.getTengxuncode());
                        //用户性别
                        fuJinRenDto.setUsersex(appUser.getSex());
                        //用户签名
                        fuJinRenDto.setQianming(appUser.getMysign());
                        //年收入
                        fuJinRenDto.setNianshouru(appUser.getNianshouru());
                        //身高
                        fuJinRenDto.setShengao(appUser.getShengao());
                        //体重
                        fuJinRenDto.setTizhong(appUser.getTizhong());
                        //职业名
                        fuJinRenDto.setZhiyename(appUser.getZhiyename());
                        //年龄
                        fuJinRenDto.setAge(getAgeByBirth(appUser.getBirthday()));
                        //在线状态
                        fuJinRenDto.setOnlinestatus(appUser.getOnlinestatus());
                        //实名认证状态
                        fuJinRenDto.setStatus(appUser.getStatus());
                        fuJinRenDtoList.add(fuJinRenDto);
                    }
                }

            } else {
                //查询redis统计条数
                Object object = redisUtil.get(RedisKeys.USER_COUNT_BOY);
                //判断是否为空
                if (ObjectUtil.isNotEmpty(object)) {
                    //查询操作
                    HashMap<String, Object> params = new HashMap<>();
                    //账号状态
                    params.put("states", 1);
                    //是否真实用户
                    params.put("isReal", 0);
                    //查询女号
                    params.put("sex", 1);
                    //获取缓存总条数记录
                    Integer countInt = Integer.valueOf(object.toString());
                    //总页数 / 每页多少条
                    int randomNum = countInt / selectPageSize + (countInt % selectPageSize != 0 ? 1 : 0);
                    Random random = new Random();
                    int nextInt = random.nextInt(randomNum);
                    SplitPageBean splitPageBean = new SplitPageBean(nextInt, selectPageSize);
                    userList = appUserService.list(params, splitPageBean);

                    for (AppUser appUser : userList) {

                        FuJinRenDto fuJinRenDto = new FuJinRenDto();
                        fuJinRenDto.setUserid(appUser.getId());
                        //用户标识
                        fuJinRenDto.setUsercode(appUser.getUsercode());
                        // 用户头像
                        fuJinRenDto.setUsericon(appUser.getPic());
                        //用户昵称
                        fuJinRenDto.setUsernick(appUser.getNick());
                        //腾讯code
                        fuJinRenDto.setTexcode(appUser.getTengxuncode());
                        //用户性别
                        fuJinRenDto.setUsersex(appUser.getSex());
                        //用户签名
                        fuJinRenDto.setQianming(appUser.getMysign());
                        //年收入
                        fuJinRenDto.setNianshouru(appUser.getNianshouru());
                        //身高
                        fuJinRenDto.setShengao(appUser.getShengao());
                        //体重
                        fuJinRenDto.setTizhong(appUser.getTizhong());
                        //职业名
                        fuJinRenDto.setZhiyename(appUser.getZhiyename());
                        //年龄
                        fuJinRenDto.setAge(getAgeByBirth(appUser.getBirthday()));
                        //在线状态
                        fuJinRenDto.setOnlinestatus(appUser.getOnlinestatus());
                        //实名认证状态
                        fuJinRenDto.setStatus(appUser.getStatus());
                        fuJinRenDtoList.add(fuJinRenDto);
                    }
                } else {

                    //查询操作
                    HashMap<String, Object> params = new HashMap<>();
                    //账号状态
                    params.put("states", 1);
                    //是否真实用户
                    params.put("isReal", 0);
                    //查询女号
                    params.put("sex", 1);
                    //获取条数
                    int countInt = appUserService.userCount(params).size();
                    //查询条数
                    redisUtil.set(RedisKeys.USER_COUNT_BOY, countInt, 3600);
                    //总页数 / 每页多少条
                    int randomNum = countInt / selectPageSize + (countInt % selectPageSize != 0 ? 1 : 0);
                    Random random = new Random();
                    int nextInt = random.nextInt(randomNum);
                    SplitPageBean splitPageBean = new SplitPageBean(nextInt, selectPageSize);
                    userList = appUserService.list(params, splitPageBean);
                    for (AppUser appUser : userList) {
                        FuJinRenDto fuJinRenDto = new FuJinRenDto();
                        fuJinRenDto.setUserid(appUser.getId());
                        //用户标识
                        fuJinRenDto.setUsercode(appUser.getUsercode());
                        // 用户头像
                        fuJinRenDto.setUsericon(appUser.getPic());
                        //用户昵称
                        fuJinRenDto.setUsernick(appUser.getNick());
                        //腾讯code
                        fuJinRenDto.setTexcode(appUser.getTengxuncode());
                        //用户性别
                        fuJinRenDto.setUsersex(appUser.getSex());
                        //用户签名
                        fuJinRenDto.setQianming(appUser.getMysign());
                        //年收入
                        fuJinRenDto.setNianshouru(appUser.getNianshouru());
                        //身高
                        fuJinRenDto.setShengao(appUser.getShengao());
                        //体重
                        fuJinRenDto.setTizhong(appUser.getTizhong());
                        //职业名
                        fuJinRenDto.setZhiyename(appUser.getZhiyename());
                        //年龄
                        fuJinRenDto.setAge(getAgeByBirth(appUser.getBirthday()));
                        //在线状态
                        fuJinRenDto.setOnlinestatus(appUser.getOnlinestatus());
                        //实名认证状态
                        fuJinRenDto.setStatus(appUser.getStatus());
                        fuJinRenDtoList.add(fuJinRenDto);
                    }
                }
            }
            return this.sucessJSON(fuJinRenDtoList);
        } catch (Exception e) {
            e.printStackTrace();
            return this.errorJSON("系统繁忙,请联系管理员！");
        }

    }


}
